"""
不好归类的杂项
"""
import datetime

dist_code = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁", "22": "吉林",
             "23": "黑龙江", "31": "上海", "32": "江苏", "33": "浙江", "34": "安徽", "35": "福建", "36": "江西", "37": "山东",
             "41": "河南", "42": "湖北","43": "湖南", "44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川",
             "52": "贵州", "53": "云南", "54": "西藏", "61": "陕西", "62": "甘肃", "63": "青海", "64": "宁夏", "65": "新疆",
             "71": "台湾", "81": "香港", "82": "澳门", "91": "国外"}


def is_id_card(id_str: str) -> bool:
    """
    校验字符串是否是合法身份证号
    :param id_str 待校验字符串
    """
    if len(id_str) != 18 or id_str[:2] not in dist_code.keys():
        print("length or district code invalid")
        return False
    _birth = id_str[6:14]  # 身份证中间8位，对应出生日期

    # 出生日期的合法性检查
    try:
        str_time = _birth[0:4] + "-" + _birth[4:6] + "-" + _birth[6:8] + " 00:00:00"
        dt = datetime.datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S")
        print(dt.timestamp())
    except ValueError:
        print("birthday invalid")
        return False

    _check_res = id_str[17]  # 身份证最后一位
    _w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    # 逐位权数
    valid_check_res_list = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
    # 身份证检验位
    _sum = 0
    # 定义累加和
    for i in range(0, 17):
        _sum = _sum + int(id_str[i]) * _w[i]
    # 逐位加权求和
    check_res_index = _sum % 11  # 取余数
    if _check_res != valid_check_res_list[check_res_index]:
        print("id card check invalid")
        return False
    return True

def gen_id_card(province_code: str, birthday: str, count: int = 1) -> list:
    """
    校验字符串是否是合法身份证号
    :param province_code 省
    :param birthday 出生日期
    :param count 生成多少个
    """
    res = list()
    for i in range(count):
        n = province_code + "0105" + birthday + str(i * 5 + 100)
        _w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        # 逐位权数
        valid_check_res_list = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
        # 身份证检验位
        _sum = 0
        # 定义累加和
        for j in range(0, 17):
            _sum = _sum + int(n[j]) * _w[j]
        # 逐位加权求和
        check_res_index = _sum % 11  # 取余数
        n = n + valid_check_res_list[check_res_index]
        res.append(n)

    return res


def str_class(cls):
    return "%s.%s" % (cls.__module__, cls.__qualname__)


def get_sub_str(origin_str: str, start_flag: str = "", end_flag: str = "", start_pos: int = 0, include_flag: int = 0):
    """
    在原始字符串中通过开始和结束标识字符串找到目标子串
    :param include_flag:
    :param origin_str:
    :param start_flag:
    :param end_flag:
    :param start_pos:
    :return:
    """
    if origin_str.find(start_flag, start_pos) == -1 or \
            origin_str.find(end_flag, origin_str.find(start_flag, start_pos)) == -1:
        return ""

    if include_flag:
        return _include_get_sub(origin_str, start_flag, end_flag, start_pos)
    else:
        return _normal_get_sub(origin_str, start_flag, end_flag, start_pos)


def _normal_get_sub(origin_str: str, start_flag: str = "", end_flag: str = "", start_pos: int = 0):
    """
    在原始字符串中通过开始和结束标识字符串找到目标子串
    :param origin_str:
    :param start_flag:
    :param end_flag:
    :param start_pos:
    :return:
    """
    if start_flag:
        sub_str_start_pos = origin_str.find(start_flag, start_pos) + len(start_flag)
    else:
        sub_str_start_pos = 0
    if end_flag:
        sub_str_end_pos = origin_str.find(end_flag, sub_str_start_pos)
    else:
        sub_str_end_pos = len(origin_str)

    return origin_str[sub_str_start_pos:sub_str_end_pos]


def _include_get_sub(origin_str: str, start_flag: str = "", end_flag: str = "", start_pos: int = 0):
    """
    在原始字符串中通过开始和结束标识字符串找到目标子串
    :param origin_str:
    :param start_flag:
    :param end_flag:
    :param start_pos:
    :return:
    """
    if start_flag:
        sub_str_start_pos = origin_str.find(start_flag, start_pos)
    else:
        sub_str_start_pos = 0
    if end_flag:
        sub_str_end_pos = origin_str.find(end_flag, sub_str_start_pos) + len(end_flag)
    else:
        sub_str_end_pos = len(origin_str)

    return origin_str[sub_str_start_pos:sub_str_end_pos]

def try_fmt_to_int(var):
    """
    尝试将变量转换为 INT 类型，成功则返回转换后的值， 出现ValueError则返回原值
    :param var:
    :return:
    """
    if isinstance(var, str) and var.isdigit():
        return int(var)
    else:
        return var

def is_str_numerical(str_var: str):
    """
    字符串是否可转换为数值, 可转换返回转换后的数值，不可转换则返回0
    支持整型和float类型
    :param str_var:
    :return:
    """
    flag = 1
    if str_var.startswith('-'):
        flag = -1
        str_var = str_var[1:]

    if str_var.isdigit():
        return flag * int(str_var)
    
    var_list = str_var.split('.')
    if len(var_list) == 2 and var_list[0].isdigit() and var_list[1].isdigit():
        return flag * float(str_var)

    return 0
